﻿CREATE PROCEDURE [dbo].[spCreateRefundTransaction]
	@OrderID int
AS
declare @TotalCost money
declare @JournalID int

select @TotalCost = SUM(Cost)
from tblRefund
where OrderID = @OrderID
	and JournalID is null

insert into tblJournal ( JournalEntryCode, Amount, CreateDate, JournalStatusCode, OrderID )
select 'R', @TotalCost, GETDATE(), 'E', @OrderID

set @JournalID = SCOPE_IDENTITY()


update tblRefund set JournalID = @JournalID
where OrderID = @OrderID
	and JournalID is null

-- Create the TransactionSubmit record
insert into tblTransactionSubmit ( JournalID, AccountID, CreateDate, SubmissionCount )
select @JournalID, AccountID, GETDATE(), 1
from tblOrderHeader
where OrderID = @OrderID


RETURN 0